package com.fqgj.sentry.controller;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.fqgj.sentry.biz.pc.PolicyExecutorBusiness;
import com.fqgj.sentry.common.annotations.VisitorAccessible;
import com.fqgj.sentry.policy.client.PolicyExecutorService;
import com.fqgj.sentry.policy.client.PolicySetService;
import com.fqgj.sentry.policy.entity.PcExecutorEntity;
import com.fqgj.sentry.policy.entity.PcPolicySetsEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: yujianfu (mr.vencnet@gmail.com)
 * Date: 2017/7/7
 * Time: 下午5:50
 */
@RestController
public class OkController {
    private static final Log logger = LogFactory.getLog(OkController.class);

    @Autowired
    private PolicyExecutorBusiness policyExecutorBusiness;

    @Autowired
    private PolicyExecutorService policyExecutorService;

    @Autowired
    private PolicySetService policySetService;


    @VisitorAccessible
    @RequestMapping("/ok")
    public String ok() {
        List<PcPolicySetsEntity> pcPolicySetsEntities = policySetService.selectList(9L);
        return "ok";
    }

    @VisitorAccessible
    @RequestMapping("/delete/redis")
    public String deleteRedis() {
        List<Long> appIdList = Arrays.asList(2L, 1L);
        for (Long aLong : appIdList) {
            List<PcPolicySetsEntity> policySetsEntities = policySetService.selectList(aLong);
            if (CollectionUtils.isEmpty(policySetsEntities)) {
                continue;
            }
            for (PcPolicySetsEntity policySetsEntity : policySetsEntities) {
                List<PcExecutorEntity> executorEntityList = policyExecutorService.selectPcCurrentAllExecutor(policySetsEntity.getId());
                if (CollectionUtils.isEmpty(executorEntityList)) {
                    continue;
                }
                for (PcExecutorEntity executorEntity : executorEntityList) {
                    policyExecutorBusiness.deleteRedis(executorEntity.getId());
                }
            }
        }

        return "ok";
    }

    @VisitorAccessible
    @RequestMapping(value = "/checkEsIndex", method = RequestMethod.GET)
    public String checkEsIndex() {
        String name = "admin-rest 测试数据 " + String.valueOf(new Random().nextInt(10000));
        return "";
    }
}


